﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Reflection;
using Ping.Core;
using log4net;

namespace Ping.Server.Cron
{
	public sealed class CronJobScheduler
	{
		private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		private readonly List<CronJob> _jobs;

		public CronJobScheduler()
		{
			_jobs = new List<CronJob>();
		}

		public void Add(CronJob job)
		{
			if (job == null)
				throw new ArgumentNullException();

			_jobs.Add(job);

			Log.InfoFormat("Scheduling cron-job '{0}' to run every {1}", job.Name, job.Interval.Format());
		}

		public void Start()
		{
			foreach (CronJob job in _jobs)
			{
				job.Start();
			}
		}

		public void Stop()
		{
			foreach (CronJob job in _jobs)
			{
				job.Stop();
			}
		}
	}
}